package com.fjd.controller;

import com.fjd.constant.MessageConstant;
import com.fjd.dto.AddressDTO;
import com.fjd.entity.CustomerAddr;
import com.fjd.result.Result;
import com.fjd.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("AddressController")
@RequestMapping("/shopping/address")
@Slf4j
@Api(tags = "地址管理")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 新增地址
     * @param addressDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public Result addAddress(@RequestBody AddressDTO addressDTO){
        addressService.addAddress(addressDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询当前用户的地址信息")
    public Result<List<CustomerAddr>> list(){
        List<CustomerAddr> list = addressService.list();
        return Result.success(list);
    }

    /**
     * 根据地址id查询地址信息
     * @param addressId
     * @return
     */
    @GetMapping("/get/{addressId}")
    @ApiOperation("根据地址id查询地址信息")
    public Result<CustomerAddr> getAddress(@PathVariable String addressId){
        CustomerAddr customerAddr = addressService.getAddress(addressId);
        return Result.success(customerAddr);
    }

    /**
     * 修改地址
     * @param addressDTO
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressDTO addressDTO){
        Boolean flag = addressService.updateAddress(addressDTO);
        if (!flag){
            return Result.error(MessageConstant.UPDATE_ADDRESS_FAIL);
        }
        return Result.success();
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @DeleteMapping("/delete/{addressId}")
    @ApiOperation("删除地址")
    public Result deleteAddress(@PathVariable String addressId){
        Boolean flag = addressService.deleteAddress(addressId);
        if (!flag){
            return Result.error(MessageConstant.DELETE_ADDRESS_FAIL);
        }
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<CustomerAddr> defaultAddress(){
        CustomerAddr customerAddr = addressService.getDefaultByCustomerId();
        return Result.success(customerAddr);
    }

    @GetMapping("/pickup/list")
    @ApiOperation("获取团长自提点")
    public Result<List<CustomerAddr>> pickupList(){
        List<CustomerAddr> customerAddrList =addressService.listpickup();
        return Result.success(customerAddrList);
    }

}
